package cn.jbit.user.service.impl;

import java.util.List;

import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import cn.jbit.util.SmsUtil;
import cn.jbit.pojo.User;
import cn.jbit.user.dao.UserMapper;
import cn.jbit.user.service.UserService;


@Service(value="userService")
@Transactional//事务
public class UserServiceImpl implements UserService{
	@Autowired
	private UserMapper userMapper;
	
	//调用短信接口
	@Override
	public void SMSLogin() {
		/**
		 * mobile    接受短信的用户手机号码
		 * tpl_id    您申请的短信模板ID，根据实际情况修改	注册：145720 登录：145721 找回密码：145722
		 * tpl_value 您设置的模板变量，根据实际情况修改
		 */
		Integer numSix = (int)((Math.random()*9+1)*100000);//生成6位随机数
		String tpl_value=numSix.toString();
		String mobile = "18670354021";
		String tpl_id = "145720";
		String result=SmsUtil.mobileQuery(mobile,tpl_id,tpl_value,"5cbe86bd899b4bb2d6cd7fa4ba08acaa");
		if(result!=null) {
			if(result.equals("1")) {
				System.out.println("发送成功");
			}else {
				System.out.println(result);
			}
		}
	}
	//新用户注册
	public String userRegister(User user) {
		int index =0;
		int index2 = 0;
		String result = "register";
		Object pwd = new SimpleHash("md5",user.getUser_password(),user.getUser_phone(),10);
		user.setUser_password(pwd.toString());
		try {
			index = userMapper.userRegister(user);
		} catch (Exception e) {
			e.printStackTrace();
		}
		//用户注册成功后 ，需要在角色用户表单中添加新增用户的角色
		try {
			//这里需要根据电话查询id
			Integer id = userMapper.findUserPhoneResId(user.getUser_phone());
			//根据id 添加相对应的角色
			index2 = userMapper.addUserLoginRole(id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (index!=1&&index2!=1) {
			//注册失败
			result = "login";
		}
		return result;
		
	}
	//查询电话号码是否存在
	@Override
	public Integer findUserPhone(String userPhone) {
		return userMapper.findUserPhone(userPhone);
	}
	//登录验证
	@Override
	public User findUserName(String userPhone) {
		return userMapper.findUserName(userPhone);
	}
	//注冊成功后給普通用戶添加权限
	@Override
	public Integer addUserLoginRole(Integer id) {
		return userMapper.addUserLoginRole(id);
	}
	//根据电话查询id
	@Override
	public Integer findUserPhoneResId(String userPhone) {
		return userMapper.findUserPhoneResId(userPhone);
	}
	//查询所有用户信息
	@Override
	public List<User> findUserAll(String userphone,Integer role_id,String username) {
		return userMapper.findUserAll(userphone,role_id,username);
	}

	//分页查询
	@Override
	public Page<User> findUserPage(Integer pageNum,String userphone,Integer role_id,String username) {
		//使用插件之后 必须查询所有信息，不然不起作用
		//pageNum为当前页
		Page<User> userPageInfo = PageHelper.startPage(pageNum,4);
		userMapper.findUserAll(userphone,role_id,username);
		return userPageInfo;
	}
	//修改
	@Override
	public Integer updateInfoId(User user) {
		if (user.getUser_password()!=null&&!user.getUser_password().equals("")) {
			Object pwd = new SimpleHash("md5",user.getUser_password(),user.getUser_phone(),10);
			user.setUser_password(pwd.toString());
			user.setUser_name("");
			return userMapper.updateInfoId(user);
		}else {
			//修改姓名
			return userMapper.updateInfoId(user);
		}
		
	}
	//根据姓名查询是否存在相同姓名
	@Override
	public Integer findNameUser(String name) {
		return userMapper.findNameUser(name);
	}
	//新增微信程序中未在数据库中注册的用户
	@Override
	public Integer addNameUser(String name, String password) {
		return userMapper.addNameUser(name, password);
	}
	//根据用户名查询id
	@Override
	public Integer findUserNameToId(String name) {
		return userMapper.findUserNameToId(name);
	}
	
}
